﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Content;

namespace Sudum
{
    class MusicController
    {

        private List<Song> MusicContainer;
        private ContentManager content;
        private int current = -1;
        private bool switching;
        private int switchTo;
        private int switchPhase;
        private float switchvar = 0f;
        private float switchspeed = 0.05f;
        private bool playing = false;


        public MusicController()
        {
            content = new ContentManager(Sudum.Me.Services);
            content.RootDirectory = "Content";
            MusicContainer = new List<Song>();
            MediaPlayer.IsRepeating = true;
        }

        /*
         * Load a song into the list
         * Songs will get incremental ID starting from 0 when loading
         */

        public void AddSong(string file)
        {
            Song newsong = content.Load<Song>(file);
            MusicContainer.Add(newsong);
        }

        // Play song first loaded song would be 0, second would be 1 ... etc
        // _smoothSwitch: true = fade in / fade out , false = cut
        public void Play(int id,bool _smoothSwitch)
        {
            if (id < MusicContainer.Count && id != current)
            {
                if (_smoothSwitch)
                {
                    switching = true;
                    switchTo = id;
                    if (playing)
                    {
                        switchPhase = 0;
                        switchvar = 1f;
                    }
                    else
                    {
                        MediaPlayer.Play(MusicContainer[id]);
                        switchvar = 0f;
                        switchPhase = 1;
                    }
                }
                else
                {
                    MediaPlayer.Stop();
                    MediaPlayer.Play(MusicContainer[id]);
                    MediaPlayer.Volume = 1f;
                    current = id;
                }
                playing = true;
            }
        }

        // Stops the song playing _smoothly will make the song fade out until full stop
        public void Stop(bool _smoothly)
        {
            if (_smoothly)
            {
                switching = true;
                switchTo = -1;
                switchPhase = 0;
            }
            else
            {
                playing = false;
                MediaPlayer.Stop();
            }
        }

        public void update(GameTime gametime)
        {
            if (switching)
            {
                if (switchPhase == 0)
                {
                    switchvar -= switchspeed * (float)gametime.ElapsedGameTime.TotalMilliseconds / 60;
                    if (switchvar <= 0f)
                    {
                        MediaPlayer.Stop();
                        if (switchTo > -1)
                        {
                            MediaPlayer.Play(MusicContainer[switchTo]);
                            switchPhase = 1;
                        }
                        else
                        {
                            switchPhase = 0;
                            switching = false;
                        }
                        current = switchTo;
                        switchvar = 0f;
                    }
                }
                else
                {
                    playing = true;
                    switchvar += switchspeed * (float)gametime.ElapsedGameTime.TotalMilliseconds / 60;
                    if (switchvar >= 1f)
                    {
                        switchvar = 1;
                        switching = false;
                    }
                }

                MediaPlayer.Volume = switchvar;
            }

        }
    }
}
